package com.xxjh.brs.redis.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtils {

    private final RedisTemplate<String, String> redisTemplate;

    @Autowired
    public RedisUtils(RedisTemplate<String, String> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    // 获取Redis集合Set的所有成员。
    public Set<String> getMembersFromSet(String key) {
        SetOperations<String, String> setOps = redisTemplate.opsForSet();
        return setOps.members(key);
    }

    // 判断Redis中某set类型的key是否有内容。
    public boolean hasContentInSet(String key) {
        BoundSetOperations<String, String> setOps = redisTemplate.boundSetOps(key);
        return setOps.size() > 0;
    }

    // 设置字符串类型的数据
    public void set(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }

    // 设置字符串类型的数据
    public void set(String key, String value, long timeout, TimeUnit unit) {
        redisTemplate.opsForValue().set(key, value, timeout, unit);
    }

    // 检查给定成员是否存在于指定的SET集合中
    public boolean isMemberTypeSet(String key, String member) {
        SetOperations<String, String> setOps = redisTemplate.opsForSet();
        return setOps.isMember(key, member);
    }

    // 删除SET集合中给定的key
    public Long RemoveMemberTypeSet(String key, String member) {
        SetOperations<String, String> setOps = redisTemplate.opsForSet();
        return setOps.remove(key, member);
    }

    // 获取字符串类型的数据
    public String get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    // 删除指定的key
    public Boolean delete(String key) {
        return redisTemplate.delete(key);
    }

    // 将一个或多个成员元素加入到集合中
    public Long addSet(String key, String... values) {
        return redisTemplate.opsForSet().add(key, values);
    }

    // 获取集合中的所有成员
    public Set<String> getSet(String key) {
        return redisTemplate.opsForSet().members(key);
    }

    // 将哈希表 key 中的域 field 的值设为 value
    public void putHash(String key, String field, Object value) {
        HashOperations<String, String, Object> opsForHash = redisTemplate.opsForHash();
        opsForHash.put(key, field, value);
    }

    // 获取存储在哈希表中指定字段的值
    public String getFromHash(String key, String field) {
        HashOperations<String, String, String> opsForHash = redisTemplate.opsForHash();
        return opsForHash.get(key, field);
    }

    // 获取哈希表中所有字段和值
    public Map<String, String> getAllFromHash(String key) {
        HashOperations<String, String, String> opsForHash = redisTemplate.opsForHash();
        return opsForHash.entries(key);
    }

    // 设置过期时间
    public Boolean expire(String key, long timeout, TimeUnit unit) {
        return redisTemplate.expire(key, timeout, unit);
    }
}
